package com.jack.network;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.util.Log;

import com.jack.network.utils.Constants;
import com.jack.network.utils.NetworkUtils;

/**
 * @author: dingfeng
 * @date: 2020-04-12
 * @since: 1.0.0
 **/
public class NetworkStateReceiver extends BroadcastReceiver {

    private NetWorkType netType;

    private NetWorkMonitorImpl mNetWorkMonitorImpl;

    public NetworkStateReceiver() {
        netType = NetWorkType.NONE;
    }

    public void setNetWorkMonitor(INetWorkMonitor monitor) {
        mNetWorkMonitorImpl = (NetWorkMonitorImpl) monitor;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null || intent.getAction() == null) {
            Log.d(Constants.TAG, "异常了");
            return;
        }

        final String action = intent.getAction();

        if (ConnectivityManager.CONNECTIVITY_ACTION.equalsIgnoreCase(action)) {
            Log.d(Constants.TAG, "网络发生改变");

            netType = NetworkUtils.getNetType(context);

            mNetWorkMonitorImpl.notifyNetwork(netType);
        }
    }

}
